﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;
using DDD;
using FloatComparer = UnitTest.TestHelper.FloatComparer;

namespace UnitTest.TestPolygonMode {
    [TestClass]
    public class TestSampler2D_R32ui_RG32ui_RGB32ui_RGBA32ui_ {

        #region Field
        GameWindow window;
        World wld;
        Graphics3D g3d;
        FrameBuffer fbuf;
        #endregion

        #region Setup
        [TestInitialize ()]
        public void MyClassInitialize () {
            window = new GameWindow ();
            wld = CreateWorld ();
            fbuf = CreateFrameBuffer ();

            g3d = Graphics3D.GetInstance ();
            g3d.Reset ();

            g3d.RenderPass += delegate {
                g3d.SetFrameBuffer (fbuf);
                g3d.SetWorld (wld);
                g3d.SetViewort (0, 0, 1, 1);
            };
        }

        [TestCleanup ()]
        public void MyClassCleanup () {
            window.Dispose ();
            window = null;
            wld = null;
        }

        private static FrameBuffer CreateFrameBuffer () {
            var fbuf = new FrameBuffer (4, 1, 0);
            var color0 = new Image2D<float> (DDD.PixelFormat.RGBA32f, 1, 1);
            var color1 = new Image2D<float> (DDD.PixelFormat.RGBA32f, 1, 1);
            var color2 = new Image2D<float> (DDD.PixelFormat.RGBA32f, 1, 1);
            var color3 = new Image2D<float> (DDD.PixelFormat.RGBA32f, 1, 1);
            fbuf.SetColorBuffer (0, color0);
            fbuf.SetColorBuffer (1, color1);
            fbuf.SetColorBuffer (2, color2);
            fbuf.SetColorBuffer (3, color3);
            return fbuf;
        }


        private World CreateWorld () {
            var cam = new Camera ();
            cam.SetPerspective (45, 1, 0.1f, 10f);

            var camNode = new Node ();
            camNode.Attach (cam);
            camNode.SetTranslation (0, 0, 1);

            var pos = new float[3] { 0, 0, 0 };
            var posArray = new VertexArray<float> (1, 3);
            posArray.Set (0, 1, pos);

            var vbuf = new VertexBuffer ();
            vbuf.AddVertexArray ("v_Position", posArray);

            var ibuf = new IndexBuffer<int> (PrimitiveType.Point, 1);
            var indices = new int[1] { 0 };
            ibuf.Set (0, 1, indices);

            var vert = @"#version 400
                         in  vec3 v_Position;
                               
                         void main () {
                             gl_Position = vec4(v_Position, 1);
                         }
                        ";
            var frag = @"#version 400
                         out vec4 frag_Color0;
                         out vec4 frag_Color1;
                         out vec4 frag_Color2;
                         out vec4 frag_Color3;
                         uniform usampler2D Texture0;
                         uniform usampler2D Texture1;
                         uniform usampler2D Texture2;
                         uniform usampler2D Texture3;
                          
                         void main () {
                             frag_Color0 = texture(Texture0, vec2(0.5,0.5));
                             frag_Color1 = texture(Texture1, vec2(0.5,0.5));
                             frag_Color2 = texture(Texture2, vec2(0.5,0.5));
                             frag_Color3 = texture(Texture3, vec2(0.5,0.5));
                         }
                        ";

            var vs = new VertexShader (new MemoryStream (Encoding.UTF8.GetBytes (vert)));
            var fs = new FragmentShader (new MemoryStream (Encoding.UTF8.GetBytes (frag)));
            var sp = new ShaderProgram (vs, fs);

            var img0 = new Image2D<uint> (DDD.PixelFormat.R32ui, 2, 2);
            var img1 = new Image2D<uint> (DDD.PixelFormat.RG32ui, 2, 2);
            var img2 = new Image2D<uint> (DDD.PixelFormat.RGB32ui, 2, 2);
            var img3 = new Image2D<uint> (DDD.PixelFormat.RGBA32ui, 2, 2);
            var pixels0 = new uint[4] { 1, 1, 1, 1 };
            var pixels1 = new uint[8] { 1, 2, 1, 2, 1, 2, 1, 2 };
            var pixels2 = new uint[12] { 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3 };
            var pixels3 = new uint[16] { 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4 };
            img0.Set (0, 0, 2, 2, pixels0);
            img1.Set (0, 0, 2, 2, pixels1);
            img2.Set (0, 0, 2, 2, pixels2);
            img3.Set (0, 0, 2, 2, pixels3);

            var sampler0 = new Sampler (SamplerType.Texture2D);
            var sampler1 = new Sampler (SamplerType.Texture2D);
            var sampler2 = new Sampler (SamplerType.Texture2D);
            var sampler3 = new Sampler (SamplerType.Texture2D);
            sampler0.SetImage (img0);
            sampler1.SetImage (img1);
            sampler2.SetImage (img2);
            sampler3.SetImage (img3);

            var unifs = new UniformArray ();
            unifs.Add ("Texture0", sampler0);
            unifs.Add ("Texture1", sampler1);
            unifs.Add ("Texture2", sampler2);
            unifs.Add ("Texture3", sampler3);

            var app = new Appearance ();
            app.SetShaderProgram (sp);
            app.AddUniformArray (unifs);
            app.AddSampler (sampler0);
            app.AddSampler (sampler1);
            app.AddSampler (sampler2);
            app.AddSampler (sampler3);

            var msh = new Mesh ();
            msh.SetVertexBuffer (vbuf);
            msh.SetIndexBuffer (ibuf);
            msh.SetAppearance (app);

            var mshNode = new Node ();
            mshNode.Attach (msh);

            var wld = new World ();
            wld.AddChild (camNode);
            wld.AddChild (mshNode);
            wld.SetActiveCamera (camNode);

            return wld;
        }

        #endregion



        #region TestMethod
        [TestMethod]
        public void TestMethod1 () {

            g3d.Render ();
            window.SwapBuffers ();

            var expected0 = new float[4] { 1, 0, 0, 1 };
            var expected1 = new float[4] { 1, 2, 0, 1 };
            var expected2 = new float[4] { 1, 2, 3, 1 };
            var expected3 = new float[4] { 1, 2, 3, 4 };
            var colors0 = new float[4];
            var colors1 = new float[4];
            var colors2 = new float[4];
            var colors3 = new float[4];

            var color0 = fbuf.GetColorBuffer (0) as Image2D<float>;
            var color1 = fbuf.GetColorBuffer (1) as Image2D<float>;
            var color2 = fbuf.GetColorBuffer (2) as Image2D<float>;
            var color3 = fbuf.GetColorBuffer (3) as Image2D<float>;
            color0.Capture ();
            color1.Capture ();
            color2.Capture ();
            color3.Capture ();
            color0.Get (0, 0, 1, 1, colors0);
            color1.Get (0, 0, 1, 1, colors1);
            color2.Get (0, 0, 1, 1, colors2);
            color3.Get (0, 0, 1, 1, colors3);

            CollectionAssert.AreEqual (expected0, colors0, new FloatComparer (0.01f));
            CollectionAssert.AreEqual (expected1, colors1, new FloatComparer (0.01f));
            CollectionAssert.AreEqual (expected2, colors2, new FloatComparer (0.01f));
            CollectionAssert.AreEqual (expected3, colors3, new FloatComparer (0.01f));
        }
        #endregion



    }
}
